package com.mall.web.control.app.user;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.mall.biz.ImageUploadBizImpl;
import com.mall.biz.user.UserBizImpl;
import com.mall.client.mall.MallService;
import com.mall.client.user.UserService;
import com.mall.common.Consts.PassJiaMi;
import com.mall.common.util.MD5Util;
import com.mall.pojo.Mall;
import com.mall.pojo.User;
import com.mall.vo.user.UserVO;
import com.mall.web.annotation.CheckCommmonOnlyKey;
import com.mall.web.annotation.UserSecurity;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/user/info/")
public class UserController {

	private Logger log = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserService userService;
	
	@Autowired
	private MallService mallService;
	@Autowired
	private UserBizImpl userBiz;
	@Autowired
	private ImageUploadBizImpl imageUploadBizImpl;

	private final String userImgPath = "user";
	
	
	
	@UserSecurity
	@RequestMapping("index")
	public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		User user = WebUtil.getCurrentUser(request);
		Mall mall = this.mallService.getMallByUserIdAndOnlyKey(user.getId(),onlyKey);
		modelMap.put("user", user);
		modelMap.put("mall", mall);
		modelMap.put("onlyKey", onlyKey);
		return "user/info/index";

	}
	@UserSecurity
	@RequestMapping("information")
	public String information(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		User user = WebUtil.getCurrentUser(request);
		Mall mall = this.mallService.getMallByUserIdAndOnlyKey(user.getId(),onlyKey);
		modelMap.put("user", user);
		modelMap.put("mall", mall);
		modelMap.put("onlyKey", onlyKey);
		return "user/info/information";

	}
	
	
	

	/**
	 * 修改密码的页面
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@CheckCommmonOnlyKey
	@RequestMapping("modifyPass")
	@UserSecurity
	public String modifyPass(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		modelMap.put("user", user);
		return "user/info/modifyPass";

	}

	/**
	 * 修改密码的接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */

	@RequestMapping("modifyPassAction")
	@ResponseBody
	@UserSecurity
	public ResponseEntity<UserVO> modifyPassAction(HttpServletRequest request, HttpServletResponse response) {
		ResponseEntity<UserVO> responseEntity = new ResponseEntity<>();

		String passWord = request.getParameter("passWord").trim();
		String newPassWord = request.getParameter("newPassWord").trim();
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		User loginUser = WebUtil.getCurrentUser(request);
		User user = this.userService.getUserByUserId(loginUser.getId());
		if (user == null) {
			responseEntity.initErrorResponseEntity(null, "原密码错误");
			return responseEntity;
		}
		if (user.getPassword().equals(MD5Util.GetMD5Code(passWord, user.getGmtCreate() + "", PassJiaMi.MD5JIAYAN))) {
			user.setPassword(MD5Util.GetMD5Code(newPassWord, user.getGmtCreate() + "", PassJiaMi.MD5JIAYAN));
			user.setGmtModify(new Date());
		} else {
			responseEntity.setCode("-200");
			responseEntity.setData(null);
			responseEntity.setMessage("modify pass error");
			return responseEntity;
		}
		this.userService.modifytUserByPrimaryKey(user);

		UserVO userVO = new UserVO();
		userVO.setUser(user);
		Boolean sessResult = userBiz.updateOrInsertSess(request, response, userVO, onlyKey);
		log.info("更新session", sessResult);
		if (!sessResult) {
			responseEntity.setCode("-300");
			responseEntity.setData(null);
			responseEntity.setMessage("sess insert or update error");
			return responseEntity;
		}
		responseEntity.setCode("200");
		responseEntity.setData(userVO);
		responseEntity.setMessage("modify pass is success");
		return responseEntity;
	}

	@UserSecurity
	@RequestMapping("modifyName")
	public String modifyName(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		modelMap.put("user", user);
		return "user/info/modifyName";
	}

	/**
	 * 修改用户的名字
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("modifyNameActoin")
	@ResponseBody
	public ResponseEntity<Boolean> modifyNameActoin(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();
		String userName = request.getParameter("userName");
		this.userBiz.updateUserNameByUser(user, userName);
		responseEntity.initSuccResponseEntity(true, "succ");
		return responseEntity;
	}

	/**
	 * 修改用户的性别
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("modifySexActoin")
	@ResponseBody
	public ResponseEntity<Boolean> modifySexActoin(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();
		User user = WebUtil.getCurrentUser(request);
		String sex = request.getParameter("sex");
		this.userBiz.updateUserSexByUser(user, sex);
		responseEntity.initSuccResponseEntity(true, "succ");
		return responseEntity;
	}
	
	/**
	 * 修改头像
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("modifyAvatar")
	public String modifyAvatar(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		modelMap.put("user", user);
		return "user/info/modifyAvatar";
	}
	

	/**
	 * 修改用户头像
	 * 
	 * @param request
	 * @param response
	 * @param file
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("modifyPortraitJson")
	@ResponseBody
	public ResponseEntity<Boolean> modifyPortraitJson(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("portrait") MultipartFile file, ModelMap modelMap) {
		ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();
		User user = WebUtil.getCurrentUser(request);
		String imgFilePath = imageUploadBizImpl.uploadImage(file, userImgPath, request);
		this.userBiz.updatePortraitByUser(user, imgFilePath);
		responseEntity.initSuccResponseEntity(true, "succ");
		return responseEntity;
	}
	
	@UserSecurity
	@RequestMapping("modifyPortraitAction")
	public String modifyPortraitAction(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("portrait") MultipartFile file, ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		String imgFilePath = imageUploadBizImpl.uploadImage(file, userImgPath, request);
		this.userBiz.updatePortraitByUser(user, imgFilePath);
		return "redirect:/user/info/information";
	}
	
	
	

}
